<?php
namespace Common\Mysql;
class LinkMysql extends CommonMysql {

    public function addLink($data) {
        $title = v($data,'title');
        verify($title,'notEmpty','友链名称不能为空');

        $url = v($data,'url');
        verify($url,'notEmpty','链接地址不能为空');
        verify($url,'isUrl','非法链接地址');

        $sid = v($data,'sid');
        verify($sid,'notEmpty','网站ID不能为空');
        verify($sid,'isID','非法网站ID');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        $sort = v($data,'sort',1,true);
        verify($sort,'isNumber','非法排序');

        if($this->where("`title`='{$title}' AND `sid` = '{$sid}'")->count()) {
            E('友链已经存在');
        }

        $siteInfo = M('Site')->find($sid);;
        verify($siteInfo,'notEmpty','网站不存在');
    
        $id = $this->addRecord(array(
            'title'     =>  $title,
            'sid'       =>  $sid,
            'url'       =>  $url,
            'sort'      =>  $sort,
            'createtime'=>  NOW_TIME,
            'status'    =>  $status,
        ),false,'友链添加失败');

        return $id;
    }

    public function updateLink($data) {
        $id = v($data,'id');
        verify($id,'notEmpty','友链ID不能为空');
        verify($id,'isID','非法友链ID');

        $title = v($data,'title');
        verify($title,'notEmpty','友链名称不能为空');

        $url = v($data,'url');
        verify($url,'notEmpty','链接地址不能为空');
        verify($url,'isUrl','非法链接地址');

        $sid = v($data,'sid');
        verify($sid,'notEmpty','网站ID不能为空');
        verify($sid,'isID','非法网站ID');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        $sort = v($data,'sort',1,true);
        verify($sort,'isNumber','非法排序');

        if($this->where("`title`='{$title}' AND `sid` = '{$sid}' AND `id` <> {$id}")->count()) {
            E('友链已经存在');
        }

        $siteInfo = M('Site')->find($sid);;
        verify($siteInfo,'notEmpty','网站不存在');
    
        $id = $this->updateRecord(array(
            'id'        =>  $id,
            'title'     =>  $title,
            'url'       =>  $url,
            'sid'       =>  $sid,
            'sort'      =>  $sort,
            'status'    =>  $status,
        ),false,'友链修改失败');

        return $id;
    }
}
?>
